/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.dao;

import cn.eclass.entity.Entity;
import cn.eclass.entity.GiftOrderEntity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author fortruth
 */
public class GiftOrderDao extends CommonDao {

    private static final String TABLE_NAME = "giftorder";

    public GiftOrderDao() {
        super(GiftOrderDao.class.getName());
    }

    @Override
    protected Entity fillEntity(ResultSet resultSet) {
        GiftOrderEntity entity = new GiftOrderEntity();
        try {
            entity.setId(resultSet.getInt("ID"));
            entity.setGiftId(resultSet.getInt("GIFT_ID"));
            entity.setUserId(resultSet.getInt("USER_ID"));
            entity.setOrdertime(resultSet.getTimestamp("ORDERTIME").getTime());
            entity.setDescription(resultSet.getString("DESCRIPTION"));
            entity.setValid(resultSet.getInt("STATUS"));
        } catch (SQLException ex) {
            logger.error(ex);
        }
        return entity;
    }

    public GiftOrderEntity add(GiftOrderEntity entity) {
        String sql = "insert into " + TABLE_NAME + " set "
                + "`USER_ID` = " + entity.getUserId() + ","
                + "`DESCRIPTION` = " + entity.getDescription() + ","
                + "`GIFT_ID` = " + entity.getGiftId() + ","
        		+ "`STATUS` = " + entity.getValid();
        int newId = super.insert(sql);
        entity.setId(newId);
        return entity;
    }

    public boolean update(GiftOrderEntity entity) {
        String sql = "update " + TABLE_NAME + " set "
                + "`USER_ID` = " + entity.getUserId() + ","
                + "`DESCRIPTION` = " + entity.getDescription() + ","
                + "`GIFT_ID` = " + entity.getGiftId() + ","
                + "`STATUS` = " + entity.getValid() 
                + " where id = " + entity.getId();

        int effectedRows = super.update(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean delete(int id) {
        String sql = "delete from " + TABLE_NAME + " where id =" + id;
        int effectedRows = super.delete(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public GiftOrderEntity get(int id) {
        String sql = "select * from " + TABLE_NAME + " where id = " + id;
        List<GiftOrderEntity> giftorders = (List<GiftOrderEntity>) super.select(sql);
        return giftorders.get(0);
    }

    public List<GiftOrderEntity> list() {
        String sql = "select * from " + TABLE_NAME;
        return (List<GiftOrderEntity>) super.select(sql);
    }

    //default order is by time.
    public List<GiftOrderEntity> getByUser(int userId) {
        String sql = "select * from " + TABLE_NAME + " where user_id = " + userId + " ORDER BY ORDERTIME DESC";
        return (List<GiftOrderEntity>) super.select(sql);
    }
}
